# buildroot set 2025.3
SHELL=/bin/bash

#需要在my24ok_f1c200s_defconfi中 设置  默认下载目录 否则默认dl目录是 linux_sdk/output/buildroot-2024.02.11/dl
#BR2_DL_DIR="$(TOPDIR)/../../dl"
 
TOP_DIR:=$(CURDIR)
BR_NAME = buildroot-2024.02.11
BR_TAR_NAME = ${BR_NAME}.tar.gz
BR_SITES = https://buildroot.org/downloads/


BR_OVERLAY_DIR =$(TOP_DIR)/buildroot-overlay

BR_OUTPUT_DIR=$(TOP_DIR)/output
BR_SRC_DIR = $(BR_OUTPUT_DIR)/$(BR_NAME)
BR_DL_DIR=$(TOP_DIR)/dl


 
# 定义 CONF 变量
ifeq ("$(origin CONF)", "command line")
$(shell echo CONF=$(CONF)>.last_conf;)
else
$(shell [ -f .last_conf ] || ( echo CONF=my24ok_f1c200s_defconfig>.last_conf;) )
endif
include .last_conf
BRW_BUILD_DIR = $(BR_OUTPUT_DIR)/$(CONF)

.PHONY: prepare sync_file  f1c200s add_lvgl_package  f1c200s_default_config distclean menuconfig

LVGL_VERSION ?= 9.2.2
lvgl_site=https://github.com/lvgl/lvgl/archive/refs/tags/v$(LVGL_VERSION).tar.gz
lvgl_dl_dir=$(BR_OVERLAY_DIR)/package/lvgl/lvgl
#创建文件夹
prepare:
#	export $(BRW_BUILD_DIR)
#	tar -cvf $(BR_OVERLAY_DIR)/package/lvgl/lvgl.tar.gz $(BR_OVERLAY_DIR)/package/lvgl/lvgl	
	@echo "make CONF=$(CONF)"
	@echo  "当前目录 :$(TOP_DIR)" 
	@echo  "DL 目录  :$(BR_DL_DIR)"
	@echo  "BR_SRC 目录  :$(BR_SRC_DIR)"
	@echo " "

	@echo "创建 $(BR_DL_DIR) $(BR_OUTPUT_DIR) $(BRW_BUILD_DIR) ... "
	$(shell mkdir -p $(BR_DL_DIR))
	$(shell mkdir -p $(BR_OUTPUT_DIR))
	$(shell mkdir -p $(BRW_BUILD_DIR))
	@echo " "

	@echo "下载 $(BR_TAR_NAME) ... "
	wget -c $(BR_SITES)$(BR_TAR_NAME) -P $(BR_DL_DIR) 
	rm -rf $(BR_SRC_DIR)  
	tar -xf $(BR_DL_DIR)/$(BR_TAR_NAME) -C $(BR_OUTPUT_DIR)

	@echo "下载 LVGL ... "
	if [ ! -f $(lvgl_dl_dir)/v$(LVGL_VERSION).tar.gz ]; then wget -c $(lvgl_site) -P $(lvgl_dl_dir);  fi;
	
	tar -xf $(lvgl_dl_dir)/v$(LVGL_VERSION).tar.gz -C $(BR_OVERLAY_DIR)/package/lvgl/lvgl
	@echo " "

	@echo "编译 F1C200S  命令 ---- make f1c200s CONF=my24ok_f1c200s_defconfig  或者 make f1c200s ---"
	@echo " "

#添加lvgl package
#add_lvgl_package:prepare
#	echo -e "menu \"LVGL\"" >> $(BR_SRC_DIR)/package/Config.in
#	echo -e "	source \"package/lvgl/Config.in\""  >> $(BR_SRC_DIR)/package/Config.in
#	echo "endmenu" >> $(BR_SRC_DIR)/package/Config.in
 

#同步文件到buildroot
sync_file:prepare  
	@echo "同步 $(BR_OVERLAY_DIR) 文件到 到 $(BR_SRC_DIR) "
	rsync -a $(BR_OVERLAY_DIR)/ $(BR_SRC_DIR)/ --exclude "*-overlay"
#	cp -af $(BR_OVERLAY_DIR)/board $(BRW_BUILD_DIR)/
	cp -af $(BR_OVERLAY_DIR)/board $(BR_OUTPUT_DIR)/
#添加lvgl package
	echo -e "menu \"LVGL\"" >> $(BR_SRC_DIR)/package/Config.in
	echo -e "	source \"package/lvgl/Config.in\""  >> $(BR_SRC_DIR)/package/Config.in
	echo "endmenu" >> $(BR_SRC_DIR)/package/Config.in

# make CONF
$(BRW_BUILD_DIR)/.config: sync_file 
	make -C $(BR_SRC_DIR) $(CONF) O=$(BRW_BUILD_DIR)
	touch $@

##配置F1C200S
#f1c200s_default_config:add_lvgl_package $(BRW_BUILD_DIR)/.config 
#	@echo 配置F1C200S
 
menuconfig:$(BRW_BUILD_DIR)/.config  
	@echo 开始配置 
	make -C $(BRW_BUILD_DIR) menuconfig

#编译f1c200s 在目录 BRW_BUILD_DIR
f1c200s:menuconfig
	@echo 开始编译f1c200s
	make -C $(BRW_BUILD_DIR) all

distclean:
	@echo 删除output目录
	rm -rf $(BR_SRC_DIR)
	rm -rf $(BRW_BUILD_DIR)